import pdf from "vue-pdf";
import JsPDF from 'pdfjs-dist';
const PDFJS = require('pdfjs-dist/build/pdf.js'); // import 会报错
window.pdfjsWorker = require('pdfjs-dist/build/pdf.worker')
import { changeUrl } from "./changeUrl"; //同目录下新建changeUrl.js文件
export function pdf_canvas(file, type, fileForm) {
	// pdf转canvas
	const onloadFile = {};
	const canvasArr = [];
	let numPages = 0;
	changeUrl(file, type, fileForm + '_url').then(perfix64 => {
		const CMAP_URL = "https://unpkg.com/pdfjs-dist@2.0.943/cmaps/";
		const src = pdf.createLoadingTask({
			url: perfix64,
			withCredentials: false,
			cMapUrl: CMAP_URL,
			cMapPacked: true
		});
		src.promise.then(pdf => {
				for (let i = 1, l = pdf.numPages; i <= l; i++) {
					numPages = pdf.numPages;
					pdf.getPage(i).then((page) => {
						const canvas = document.createElement("canvas");
						const ctx = canvas.getContext('2d');
						const viewport = page.getViewport({ scale: 1.5 });
						canvas.height = viewport.height;
						canvas.width = viewport.width;
						canvas.style.width = viewport.width + 'px';
						canvas.style.height = viewport.height + 'px';
						page.render({
							canvasContext: ctx,
							viewport,
						});
						canvasArr.push(canvas);
						if (numPages == i) {
							onloadFile.Callback ? onloadFile.Callback(canvasArr) : '';
						}
					});
				}
			})
			.catch(err => {
				console.error("pdf 加载失败", err);
			});
	});
	return onloadFile
}
export function pdf_img(file, type, fileForm) {
	console.log(123)
	// pdf转img
	const onloadFile = {};
	const imgList = [];
	const canvas = pdf_canvas(file, type, fileForm);
	canvas.Callback = function(canvasArr) {
		setTimeout(() => {
			for (let i = 0; i < canvasArr.length; i++) {
				imgList.push(canvasArr[i].toDataURL("image/png"));
			}
			onloadFile.Callback ? onloadFile.Callback(imgList) : '';
		}, 1000);
	}
	console.log(imgList, 'onloadFileonloadFileonloadFile')
	return imgList;
}

export function img_canvas(file, fileForm) {
	// img转canvas
	const onloadFile = {};
	changeUrl(file, fileForm, fileForm + '_url').then(url => {
		const img = new Image(); //创建一个临时存储
		img.src = url //将图片数据赋值即可
		img.onload = function() { //定义加载图片后弹出显示
			const canvas = document.createElement("canvas");
			canvas.width = img.width;
			canvas.height = img.height;
			this.ctx = canvas.getContext("2d");
			this.ctx.drawImage(img, 0, 0, img.width, img.height);
			onloadFile.Callback ? onloadFile.Callback(canvas) : '';
			img.remove();
		};
	})
	return onloadFile;
}

export function img_pdf(file, fileForm) {
	// img转pdf
	const onloadFile = {};
	const canvas = img_canvas(file, fileForm);
	canvas.Callback = function(canvas) {
		const base64pdf = canvas_pdf(canvas);
		onloadFile.Callback ? onloadFile.Callback(base64pdf) : '';
	}
	return onloadFile;
}

export function canvas_pdf(canvas) {
	// canvas转pdf
	const canvasUrl = canvas.toDataURL('image/jpeg');
	const pdf = new JsPDF('x', 'px', [canvas.width, canvas.height]);
	pdf.addImage(canvasUrl, 'PNG', 0, 0, canvas.width, canvas.height);
	let base64pdf = pdf.output('datauristring');
	base64pdf = base64pdf.replace('data:application/pdf;filename=generated.pdf;base64,', 'data:application/pdf;base64,');
	return base64pdf;
}